<?php
/**
 * 要求wordpress版本在4.1以上
 */
if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) {
	require get_template_directory() . '/inc/back-compat.php';
}

/*
* 初始化滑稽pro
*/
if ( ! function_exists( 'zhuanyunhu_setup' ) ) :
function zhuanyunhu_setup() {

    /*
	* i18n
	*/
	load_theme_textdomain( 'zhuanyunhu', get_template_directory() . '/languages' );

    /*
	* 支持自定义标题
	*/
	add_theme_support( 'title-tag' );

    /*
	* 支持站点logo
	*/
	add_theme_support( 'custom-logo', array(
		'height'      => 248,
		'width'       => 248,
		'flex-height' => true,
	) );

    /*
	 * 编辑器样式
	 */
	add_editor_style( array( 'Bootstrap/css/bootstrap.min.css') );

    /*
	 * 小工具选择性刷新
	 */
	add_theme_support( 'customize-selective-refresh-widgets' );

	//注册菜单
	register_nav_menus( array(
		'top_left' => __( '顶部菜单',      'zhuanyunhu' ),
	) );
}
endif;
add_action( 'after_setup_theme', 'zhuanyunhu_setup' );

function timeago( $ptime ) {
    $ptime = strtotime($ptime);
    $etime = time() - $ptime;
    if($etime < 1) return '刚刚';
    $interval = array (
        12 * 30 * 24 * 60 * 60  =>  '年前 ('.date('Y-m-d', $ptime).')',
        30 * 24 * 60 * 60       =>  '个月前 ('.date('m-d', $ptime).')',
        7 * 24 * 60 * 60        =>  '周前 ('.date('m-d', $ptime).')',
        24 * 60 * 60            =>  '天前',
        60 * 60                 =>  '小时前',
        60                      =>  '分钟前',
        1                       =>  '秒前'
    );
    foreach ($interval as $secs => $str) {
        $d = $etime / $secs;
        if ($d >= 1) {
            $r = round($d);
            return $r . $str;
        }
    };
}

function zhuanyunhu_scripts_styles(){
	wp_enqueue_style( 'zhuanyunhu-style', get_stylesheet_uri() );

	//ie9以下支持
	wp_enqueue_style( 'html5shiv', 'http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js',array(),'3.7.2' );
	wp_style_add_data( 'html5shiv', 'conditional', 'lt IE 9' );
	wp_enqueue_style( 'respond', 'http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js',array(),'1.4.2' );
	wp_style_add_data( 'respond', 'conditional', 'lt IE 9' );

	wp_enqueue_script('theme-script',get_template_directory_uri() . '/js/view.js',array('jquery'),'0.0.1',true);
	wp_enqueue_script('tinymce',get_template_directory_uri() . '/js/tinymce.min.js',array('jquery'),'0.0.1',false);
}
add_action( 'wp_enqueue_scripts', 'zhuanyunhu_scripts_styles' );

//注册数据
add_action('admin_init', 'register_theme_settings');
function register_theme_settings() 
{ 
    register_setting("theme_mods_freshblog","theme_mods_freshblog");
}//添加admin外观菜单
add_action('admin_menu', 'add_theme_options_menu');
function add_theme_options_menu() { 
    add_theme_page('广告设置','广告选项','edit_theme_options','theme-options', 'theme_settings_admin');
}


function theme_settings_admin() { //这里写选项页面内容
    if ( !empty($_POST) && check_admin_referer('theme_options-update') ) {
		$ii = 1;
		while(array_key_exists('left-ad'.$ii,$_POST)){
			update_option('left-ad'.$ii,$_POST['left-ad'.$ii]);
			update_option('left-ad'.$ii.'-link',$_POST['left-ad'.$ii.'-link']);
			update_option('left-ad'.$ii.'-text',$_POST['left-ad'.$ii.'-text']);
			$ii++;
		}
		if($_POST['submit'] == "删除一个左侧广告栏"){
			delete_option('left-ad'.($ii - 1));
			delete_option('left-ad'.($ii - 1).'-link');
			delete_option('left-ad'.($ii - 1).'-text');
		}

		$ii = 1;
		while(array_key_exists('right-ad'.$ii,$_POST)){
			update_option('right-ad'.$ii,$_POST['right-ad'.$ii]);
			update_option('right-ad'.$ii.'-link',$_POST['right-ad'.$ii.'-link']);
			update_option('right-ad'.$ii.'-text',$_POST['right-ad'.$ii.'-text']);
			$ii++;
		}
		if($_POST['submit'] == "删除一个右侧广告栏"){
			delete_option('right-ad'.($ii - 1));
			delete_option('right-ad'.($ii - 1).'-link');
			delete_option('right-ad'.($ii - 1).'-text');
		}

		$ii = 1;
		while(array_key_exists('lb-ad'.$ii,$_POST)){
			update_option('lb-ad'.$ii,$_POST['lb-ad'.$ii]);
			update_option('lb-ad'.$ii.'-link',$_POST['lb-ad'.$ii.'-link']);
			update_option('lb-ad'.$ii.'-text',$_POST['lb-ad'.$ii.'-text']);
			$ii++;
		}
		if($_POST['submit'] == "删除一个轮播"){
			delete_option('lb-ad'.($ii - 1));
			delete_option('lb-ad'.($ii - 1).'-link');
			delete_option('lb-ad'.($ii - 1).'-text');
		}

		update_option('top-ad',$_POST['top-ad']);
		update_option('top-ad-link',$_POST['top-ad-link']);
		update_option('about',$_POST['about']);
		update_option('right-top',$_POST['right-top']);
		update_option('qq-1',$_POST['qq-1']);
		update_option('qq-2',$_POST['qq-2']);
		update_option('qq-3',$_POST['qq-3']);
		update_option('qq-4',$_POST['qq-4']);
		echo '<div id="message" class="updated notice is-dismissible"><p>保存成功。</p></div>';
    }
    ?>
    <div class="wrap">
        <h2>广告设置</h2>
        <form action="" method="post" id="theme-options-form">
					<?php 
					$left = array();
					$ii = 1;
					$iii = 1;
					while(true){
						$flag = get_option("left-ad".$ii++);
						if($flag){
							array_push($left,$flag);
						}else{
							break;
						}
					}
					foreach($left as $c){
					 ?>
					<h3 class="ad-<?php echo $iii ?>"><strong>左侧广告栏<?php echo $iii ?>：</strong></h3>
					<table class="form-table ad-<?php echo $iii ?>" style="transform: translateX(30px);">
						<tbody>
							<tr>
								<th scope="row"><label for="left-ad<?php echo $iii ?>">图片:</label></th>
								<td><input type="text" id="left-ad<?php echo $iii ?>" class="ad-img regular-text" name="left-ad<?php echo $iii ?>" value="<?php echo get_option('left-ad'.$iii) ?>"/><a id="left-ad<?php echo $iii ?>" class="ashu_upload_button button" href="#">上传图片</a></td>
							</tr>
							<tr>
								<th scope="row"><label for="left-ad<?php echo $iii ?>">链接:</label></th>
								<td><input type="text" id="left-ad<?php echo $iii ?>-link" class="regular-text" name="left-ad<?php echo $iii ?>-link" value="<?php echo get_option('left-ad'.$iii.'-link') ?>"/></td>
							</tr>
							<tr>
								<th scope="row"><label for="left-ad<?php echo $iii ?>">文字:</label></th>
								<td><input type="text" id="left-ad<?php echo $iii ?>-text" class="regular-text" name="left-ad<?php echo $iii ?>-text" value="<?php echo get_option('left-ad'.$iii.'-text') ?>"/></td>
							</tr>
						</tbody>
					</table>
					<?php 
					$iii++;
					}
					?>
			<p><a id="add-left" class="button">添加左侧广告栏</a></p>
			<p><input type="submit" name="submit" id="submit" class="button button-primary" onclick="jQuery('#delete').val(true)" value="删除一个左侧广告栏"></p>

			<?php 
					$right = array();
					$ii = 1;
					$iii = 1;
					while(true){
						$flag = get_option("right-ad".$ii++);
						if($flag){
							array_push($right,$flag);
						}else{
							break;
						}
					}
					foreach($right as $c){
					 ?>
					<h3 class="ad-<?php echo $iii ?>"><strong>右侧广告栏<?php echo $iii ?>：</strong></h3>
					<table class="form-table ad-<?php echo $iii ?>" style="transform: translateX(30px);">
						<tbody>
							<tr>
								<th scope="row"><label for="right-ad<?php echo $iii ?>">图片:</label></th>
								<td><input type="text" id="right-ad<?php echo $iii ?>" class="ad-right-img regular-text" name="right-ad<?php echo $iii ?>" value="<?php echo get_option('right-ad'.$iii) ?>"/><a id="right-ad<?php echo $iii ?>" class="ashu_upload_button button" href="#">上传图片</a></td>
							</tr>
							<tr>
								<th scope="row"><label for="right-ad<?php echo $iii ?>">链接:</label></th>
								<td><input type="text" id="right-ad<?php echo $iii ?>-link" class="regular-text" name="right-ad<?php echo $iii ?>-link" value="<?php echo get_option('right-ad'.$iii.'-link') ?>"/></td>
							</tr>
							<tr>
								<th scope="row"><label for="right-ad<?php echo $iii ?>">文字:</label></th>
								<td><input type="text" id="right-ad<?php echo $iii ?>-text" class="regular-text" name="right-ad<?php echo $iii ?>-text" value="<?php echo get_option('right-ad'.$iii.'-text') ?>"/></td>
							</tr>
						</tbody>
					</table>
					<?php 
					$iii++;
					}
					?>
			<p><a id="add-right" class="button">添加右侧广告栏</a></p>
			<p><input type="submit" name="submit" id="submit" class="button button-primary" onclick="jQuery('#delete').val(true)" value="删除一个右侧广告栏"></p>

						<?php 
					$lb = array();
					$ii = 1;
					$iii = 1;
					while(true){
						$flag = get_option("lb-ad".$ii++);
						if($flag){
							array_push($lb,$flag);
						}else{
							break;
						}
					}
					foreach($lb as $c){
					 ?>
					<h3 class="ad-<?php echo $iii ?>"><strong>轮播<?php echo $iii ?>：</strong></h3>
					<table class="form-table ad-<?php echo $iii ?>" style="transform: translateX(30px);">
						<tbody>
							<tr>
								<th scope="row"><label for="lb-ad<?php echo $iii ?>">图片:</label></th>
								<td><input type="text" id="lb-ad<?php echo $iii ?>" class="ad-lb-img regular-text" name="lb-ad<?php echo $iii ?>" value="<?php echo get_option('lb-ad'.$iii) ?>"/><a id="lb-ad<?php echo $iii ?>" class="ashu_upload_button button" href="#">上传图片</a></td>
							</tr>
							<tr>
								<th scope="row"><label for="lb-ad<?php echo $iii ?>">链接:</label></th>
								<td><input type="text" id="lb-ad<?php echo $iii ?>-link" class="regular-text" name="lb-ad<?php echo $iii ?>-link" value="<?php echo get_option('lb-ad'.$iii.'-link') ?>"/></td>
							</tr>
							<tr>
								<th scope="row"><label for="lb-ad<?php echo $iii ?>">文字:</label></th>
								<td><input type="text" id="lb-ad<?php echo $iii ?>-text" class="regular-text" name="lb-ad<?php echo $iii ?>-text" value="<?php echo get_option('lb-ad'.$iii.'-text') ?>"/></td>
							</tr>
						</tbody>
					</table>
					<?php 
					$iii++;
					}
					?>
			<p><a id="add-lb" class="button">添加轮播</a></p>
			<p><input type="submit" name="submit" id="submit" class="button button-primary" onclick="jQuery('#delete').val(true)" value="删除一个轮播"></p>

			<h3 class="top-ad"><strong>顶部广告栏：</strong></h3>
			<table class="form-table top-ad" style="transform: translateX(30px);">
				<tbody>
					<tr>
						<th scope="row"><label for="top-ad">图片:</label></th>
						<td><input type="text" id="top-ad" class="regular-text" name="top-ad" value="<?php echo get_option('top-ad') ?>"/><a id="top-ad" class="ashu_upload_button button" href="#">上传图片</a></td>
					</tr>
					<tr>
						<th scope="row"><label for="top-ad-link">链接:</label></th>
						<td><input type="text" id="top-ad-link" class="regular-text" name="top-ad-link" value="<?php echo get_option('top-ad-link') ?>"/></td>
					</tr>
				</tbody>
			</table>
			<table class="form-table top-ad">
				<tbody>
					<tr>
						<th scope="row"><label for="about">中上：</label></th>
						<td><input type="text" id="about" class="regular-text" name="about" value="<?php echo get_option('about') ?>"/></td>
					</tr>
					<tr>
						<th scope="row"><label for="right-top">右上：</label></th>
						<td><input type="text" id="right-top" class="regular-text" name="right-top" value="<?php echo get_option('right-top') ?>"/></td>
					</tr>
					<tr>
						<th scope="row"><label for="qq-1">联系qq1：</label></th>
						<td><input type="text" id="qq-1" class="regular-text" name="qq-1" value="<?php echo get_option('qq-1') ?>"/></td>
					</tr>
					<tr>
						<th scope="row"><label for="qq-2">联系qq2：</label></th>
						<td><input type="text" id="qq-2" class="regular-text" name="qq-2" value="<?php echo get_option('qq-2') ?>"/></td>
					</tr>
					<tr>
						<th scope="row"><label for="qq-3">联系qq3：</label></th>
						<td><input type="text" id="qq-3" class="regular-text" name="qq-3" value="<?php echo get_option('qq-3') ?>"/></td>
					</tr>
					<tr>
						<th scope="row"><label for="qq-4">联系qq4：</label></th>
						<td><input type="text" id="qq-4" class="regular-text" name="qq-4" value="<?php echo get_option('qq-4') ?>"/></td>
					</tr>
				</tbody>
			</table>
			<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="保存更改"></p>
            <?php wp_nonce_field('theme_options-update'); ?>
        </form>
    </div>
    <?php
	 wp_enqueue_media(); //在设置页面需要加载媒体中心   
	 ?>
	 <script>
		jQuery(document).ready(function(){   
		var ashu_upload_frame;   
		var value_id;
		jQuery('#add-lb').click(function(){
			var times = jQuery('.ad-lb-img').length + 1;
			jQuery(this).parent().before('<h3 class="ad-' + times + '"><strong>轮播' + times + '：</strong></h3><table class="form-table ad-' + times + '" style="transform: translateX(30px);"><tbody><tr><th scope="row"><label for="lb-ad' + times + '">图片:</label></th><td><input type="text" id="lb-ad' + times + '" class="regular-text ad-lb-img" name="lb-ad' + times + '" value=""/><a id="lb-ad' + times + '" class="ashu_upload_button button" href="#">上传图片</a></td></tr><tr><th scope="row"><label for="lb-ad' + times + '">链接:</label></th><td><input type="text" id="lb-ad' + times + '-link" class="regular-text" name="lb-ad' + times + '-link" value=""/></td></tr><tr><th scope="row"><label for="lb-ad' + times + '">文字:</label></th><td><input type="text" id="lb-ad' + times + '-text" class="regular-text" name="lb-ad' + times + '-text" value=""/></td></tr></tbody></table>');
		});
		jQuery('#add-left').click(function(){
			var times = jQuery('.ad-img').length + 1;
			jQuery(this).parent().before('<h3 class="ad-' + times + '"><strong>左侧广告栏' + times + '：</strong></h3><table class="form-table ad-' + times + '" style="transform: translateX(30px);"><tbody><tr><th scope="row"><label for="left-ad' + times + '">图片:</label></th><td><input type="text" id="left-ad' + times + '" class="regular-text ad-img" name="left-ad' + times + '" value=""/><a id="left-ad' + times + '" class="ashu_upload_button button" href="#">上传图片</a></td></tr><tr><th scope="row"><label for="left-ad' + times + '">链接:</label></th><td><input type="text" id="left-ad' + times + '-link" class="regular-text" name="left-ad' + times + '-link" value=""/></td></tr><tr><th scope="row"><label for="left-ad' + times + '">文字:</label></th><td><input type="text" id="left-ad' + times + '-text" class="regular-text" name="left-ad' + times + '-text" value=""/></td></tr></tbody></table>');
		});
		jQuery('#add-right').click(function(){
			var times = jQuery('.ad-right-img').length + 1;
			jQuery(this).parent().before('<h3 class="ad-' + times + '"><strong>右侧广告栏' + times + '：</strong></h3><table class="form-table ad-' + times + '" style="transform: translateX(30px);"><tbody><tr><th scope="row"><label for="right-ad' + times + '">图片:</label></th><td><input type="text" id="right-ad' + times + '" class="regular-text ad-right-img" name="right-ad' + times + '" value=""/><a id="right-ad' + times + '" class="ashu_upload_button button" href="#">上传图片</a></td></tr><tr><th scope="row"><label for="right-ad' + times + '">链接:</label></th><td><input type="text" id="right-ad' + times + '-link" class="regular-text" name="right-ad' + times + '-link" value=""/></td></tr><tr><th scope="row"><label for="right-ad' + times + '">文字:</label></th><td><input type="text" id="right-ad' + times + '-text" class="regular-text" name="right-ad' + times + '-text" value=""/></td></tr></tbody></table>');
		});
		jQuery('.ashu_upload_button').live('click',function(event){   
			value_id =jQuery( this ).attr('id');       
			event.preventDefault();   
			if( ashu_upload_frame ){   
				ashu_upload_frame.open();   
				return;   
			}   
			ashu_upload_frame = wp.media({   
				title: '选择图片',   
				button: {   
					text: '确定',   
				},   
				multiple: false   
			});   
			ashu_upload_frame.on('select',function(){   
				attachment = ashu_upload_frame.state().get('selection').first().toJSON();   
				//jQuery('#'+value_id+'_input').val(attachment.url).trigger('change');   
				jQuery('input[name='+value_id+']').val(attachment.url).trigger('change');   
			});   
			
			ashu_upload_frame.open();   
		});   
		});   
    </script>   
	 <?php
}

//输出缩略图地址
function post_thumbnail_src(){
    global $post;
	if( $values = get_post_custom_values("thumb") ) {	//输出自定义域图片地址
		$values = get_post_custom_values("thumb");
		$post_thumbnail_src = $values [0];
	} elseif( has_post_thumbnail() ){    //如果有特色缩略图，则输出缩略图地址
        $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
		$post_thumbnail_src = $thumbnail_src [0];
    } else {
		$post_thumbnail_src = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		$post_thumbnail_src = $matches [1] [0];   //获取该图片 src
		if(empty($post_thumbnail_src)){	//如果日志中没有图片，则显示随机图片
			$random = mt_rand(1, 10);
			echo get_bloginfo('template_url');
			echo '/img/pic/'.$random.'.jpg';
			//如果日志中没有图片，则显示默认图片
			//echo '/img/thumbnail.png';
		}
	};
	echo $post_thumbnail_src;
}


if (!function_exists('df_disable_admin_bar')) { 
	function df_disable_admin_bar() { 
	remove_action('admin_footer', 'wp_admin_bar_render', 1000);
	remove_action('wp_footer', 'wp_admin_bar_render', 1000); 
	function remove_admin_bar_style_backend() { echo ''; } 
	add_filter('admin_head','remove_admin_bar_style_backend'); 
	function remove_admin_bar_style_frontend() { echo ''; } 
	add_filter('wp_head','remove_admin_bar_style_frontend', 99); 
}
} 
add_action('init','df_disable_admin_bar');

function custom_login() {  
	echo '<link rel="stylesheet" tyssspe="text/css" href="' . get_bloginfo('template_directory') . '/alogin.css" />'; 
}  

add_action('login_head', 'custom_login');

add_filter( 'bbp_verify_nonce_request_url', 'my_bbp_verify_nonce_request_url', 999, 1 );
function my_bbp_verify_nonce_request_url( $requested_url )
{
    return 'http://localhost:9090' . $_SERVER['REQUEST_URI'];
}  

function bbp_enable_visual_editor( $args = array() ) {
    $args['tinymce'] = true;
    return $args;
}
add_filter( 'bbp_after_get_the_content_parse_args', 'bbp_enable_visual_editor'); 

?>